package com.project.web.order.controller.app.AppNative;

import com.google.gson.Gson;
import com.project.common.core.utils.StringUtil;
import com.project.common.core.utils.exception.Result;
import com.project.web.order.controller.common.BaseController;
import com.project.web.order.model.app.MbrOrder;
import com.project.web.order.model.app.vo.MbrOrderInfoVO;
import com.project.web.order.model.app.vo.MbrOrderPayVO;
import com.project.web.order.service.app.MbrOrderService;
import com.project.web.util.exception.ORDER_RESP_CODE_ENUM;
import com.project.web.util.exception.OrderCustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * App会员订单API
 *
 * @Author: wyy
 * @Date: 2021-02-02 02:24
 */
@Api(tags = "【App】App会员订单API")
@RestController("AppMbrOrderController")
@RequestMapping("/order/auth/mbrOrder/app")
public class MbrAppOrderController extends BaseController {
    Logger logger = LoggerFactory.getLogger(MbrAppOrderController.class);

    @Resource(name = "mbrOrderServiceImpl")
    private MbrOrderService mbrOrderService;

    /**
     * APP会员预下单API
     *
     * @param mbrOrderPayVO 会员APP订单
     * @return
     */
    @ApiOperation(value = "APP会员预下单API")
    @PostMapping(value = "/unifiedOrder")
    public Result<Map<String, Object>> unifiedOrder(@RequestBody MbrOrderPayVO mbrOrderPayVO, HttpServletRequest httpRequest) {
        // 参数校验
        String userId = mbrOrderPayVO.getUserId();
        BigDecimal actualAmt = mbrOrderPayVO.getActualAmt();
        BigDecimal totalAmt = mbrOrderPayVO.getTotalAmt();
        Integer payMode = mbrOrderPayVO.getPayMode();
        if (mbrOrderPayVO == null || StringUtils.isBlank(userId) || actualAmt == null || totalAmt == null || payMode == null) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }
        log.info("\r\n ************** App统一下单参数  \r\n");
        log.info("\r\n {}  \r\n",new Gson().toJson(mbrOrderPayVO));
        // 保存订单
        Map<String, Object> returnMap = mbrOrderService.handleMbrOrderOfApp(mbrOrderPayVO, httpRequest);
        return getResult(returnMap);
    }

    /**
     * 查询所有订单【个人中心】
     *
     * @param mbrOrder 课程订单对象
     * @return
     */
    @ApiOperation(value = "查询所有订单【个人中心】", notes = "查询所有订单【个人中心】", httpMethod = "GET")
    @GetMapping(value = "/getOrdersOfPersonalCenter")
    public Result<List<MbrOrderInfoVO>> getOrdersOfPersonalCenter(MbrOrder mbrOrder) {
        // 校验参数
        String userId = mbrOrder.getUserId();
        if (StringUtil.isEmpty(userId)) {
            throw new OrderCustomException(ORDER_RESP_CODE_ENUM.MISS_PARAMETER);
        }

        // 查询订单
        List<MbrOrderInfoVO> mbrOrders = mbrOrderService.getOrdersOfPersonalCenter(mbrOrder);
        return getResult(mbrOrders);
    }
}
